package com.cqc.jdk7;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ExceptionTest {

    @Test
    public void f1() {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(""));
        } catch (FileNotFoundException | IllegalArgumentException e) {
            System.out.println(e);
        }

    }

    @Test
    public void f2() throws IOException {
        String result = this.read("C:\\Users\\cuiquancheng\\Desktop\\a.txt");
        System.out.println(result);
    }

    /**
     * try(stream，不需要手动关闭流){
     * 业务逻辑，
     * }
     */
    public String read(String filename) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(String.format("%n"));
            }
            return builder.toString();
        }
    }
}
